package bb.lanxing.util.ui

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import bb.lanxing.databinding.LayoutDropdownChooseBinding


class DropDownPopWindow(
    context: Context,
    list: List<DropDownItem>,
    private val listener: DropDownClickListener,
) : PopupWindow(context) {
    interface DropDownClickListener {
        fun onMenuDismissed()

        fun onMenuItemClicked(dropDownItem: DropDownItem)
    }

    init {
        val inflate: LayoutDropdownChooseBinding = LayoutDropdownChooseBinding.inflate(
            LayoutInflater.from(context)
        )
        contentView = inflate.root
        width = -2
        height = -2
        setBackgroundDrawable(null)
        elevation = 8.0f
        isOutsideTouchable = true
        isFocusable = true
        inflate.recyclerView.adapter = DropDownItemAdapter(list) {
            listener.onMenuItemClicked(it)
            dismiss()
        }
    }

    fun show(anchor: View, z: Boolean) {
        if (z) {
            width = anchor.width
        }
        showAsDropDown(anchor)
    }
}